package com.jaxb;

import java.util.List;

import javax.xml.bind.JAXBElement;

import com.jaxb.binder.BtStoneBinder;
import com.jaxb.binder.SimpleStoneBinder;

public class Test {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		//testSingleObject();
		testMulityObject();
		
	}
	
	private static void testSingleObject() {
		Book book = new Book();
		book.setContent("content");
		book.setPage("page");
		
		Message<Book> message = new Message<Book>();
		message.setObj(book);
		
		ResponseData responseData = new ResponseData();
		responseData.setReturnCode("001");
		responseData.setMessage(message);
		
		JAXBElement<ResponseData> element = new ObjectFactory().createResponseData(responseData);
		
		BtStoneBinder binder = new SimpleStoneBinder();
		String str = binder.marshall(element, ObjectFactory.class);
		System.out.println(str);
		ResponseData ResponseDataReturn = binder.unmarshall(str, ObjectFactory.class);
		System.out.println("returncode:" 
				+ ResponseDataReturn.getReturnCode()
				+ "\n"
				+ "bookcontent1:"
				+ ((Book)ResponseDataReturn.getMessage().getObj()).getContent());
	}
	
	private static void testMulityObject() {
		Book book1 = new Book();
		book1.setContent("content1");
		book1.setPage("page1");
		
		Book book2 = new Book();
		book2.setContent("content2");
		book2.setPage("page2");
		
		ListWrapper<Book> listWrapper = new ListWrapper<Book>();
		List<Book> list = listWrapper.getList();
		list.add(book1);
		list.add(book2);
		
		Message<ListWrapper<Book>> message = new Message<ListWrapper<Book>>();
		message.setObj(listWrapper);
		
		ResponseData responseData = new ResponseData();
		responseData.setReturnCode("001");
		responseData.setMessage(message);
		
		JAXBElement<ResponseData> element = new ObjectFactory().createResponseData(responseData);
		
		BtStoneBinder binder = new SimpleStoneBinder();
		String str = binder.marshall(element, ObjectFactory.class);
		System.out.println(str);
		ResponseData ResponseDataReturn = binder.unmarshall(str, ObjectFactory.class);
		System.out.println("returncode:" 
				+ ResponseDataReturn.getReturnCode()
				+ "\n"
				+ "bookcontent2:"
				+ ((ListWrapper<Book>)ResponseDataReturn.getMessage().getObj()).getList().get(1).getContent());
	}

}
